/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-12-01     Administrator       the first version
 */
#include <rtthread.h>
#include "board.h"
#include "adc.h"
#include "PWM.h"
#define ADC_DEV_NAME        "adc0"      /* ADC 设备名称 */
#define ADC_DEV_CHANNEL     4        /* ADC 通道 */
#define REFER_VOLTAGE       3300         /* 参考电压 3.3V,数据精度乘以1000保留3位小数*/
#define ADC_TEMPER_CHANNEL    3       /* ADC 温度通道 */
#define REFER_temperature       3300
#define CONVERT_BITS        (1 << 10)   /* 转换位数为10位 */
extern  rt_flag_t power;

  void adc_measure(void *p)
{
    rt_err_t ret = RT_EOK;
       rt_uint32_t value[10], value_sum,vol,vol_ave,ii,jj,temp;
       rt_adc_device_t adc_dev;

       while(1)
       {
          adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
          if (adc_dev == RT_NULL)
          {
              rt_kprintf("can't find %s device!\n", ADC_DEV_NAME);
              return ;
          }

          ret = rt_adc_enable(adc_dev, ADC_DEV_CHANNEL);

          for(ii=0;ii<10;ii++)
          {
              value[ii] = rt_adc_read(adc_dev, ADC_DEV_CHANNEL);
          }

//          for(ii=0;ii<10;ii++)
//          {
//              rt_kprintf("%d;",value[ii]);
//          }

          for(ii=0;ii<10;ii++)//排序
          {
          for(jj=ii+1;jj<10;jj++)
          {
              if(value[jj]<value[ii])
              {
                  temp=value[ii];
                  value[ii]=value[jj];
                  value[jj]=temp;
              }
          }
          }

//          rt_kprintf(" \r\n");
//          for(ii=0;ii<10;ii++)
//                  {
//                      rt_kprintf("%d;",value[ii]);
//                  }
//          rt_kprintf(" \r\n");

          for(ii=1;ii<9;ii++)     //去掉最高和最低求平均 数组中间8位
          {
              value_sum += value[ii];
        //      rt_kprintf("%d;",value[ii]);
          }
          vol_ave =value_sum /8;
          vol=vol_ave* REFER_VOLTAGE / CONVERT_BITS;
        //  rt_kprintf("the vol is :%d \n", vol);
          rt_kprintf(" Voltage = %d.%03d \n", vol / 1000, vol % 1000);
          value_sum=0;
          ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL);
          rt_thread_mdelay(100);
          if(vol>1000)//过载保护，估计是10A左右，到时再测
                  {
              //停机，报警，故障码

              pwm_vout_adj(10,0);//超温报警
              rt_kprintf("Warning, excessive power!!!\r\n");
                  }
  ////////////////////////////*温度测量*//////////////////////////////////////////////////////
          ret = rt_adc_enable(adc_dev, ADC_TEMPER_CHANNEL);//温度测量

                 for(ii=0;ii<10;ii++)
                 {
                     value[ii] = rt_adc_read(adc_dev, ADC_TEMPER_CHANNEL);
                 }
                 for(ii=0;ii<10;ii++)//排序
                 {
                 for(jj=ii+1;jj<10;jj++)
                 {
                     if(value[jj]<value[ii])
                     {
                         temp=value[ii];
                         value[ii]=value[jj];
                         value[jj]=temp;
                     }
                 }
                 }

                 for(ii=1;ii<9;ii++)     //去掉最高和最低求平均 数组中间8位
                 {
                     value_sum += value[ii];
               //      rt_kprintf("%d;",value[ii]);
                 }
                 vol_ave =value_sum /8;
                 vol=vol_ave* REFER_VOLTAGE / CONVERT_BITS;
               //  rt_kprintf("the vol is :%d \n", vol);
                 rt_kprintf(" Temperature = %d.%03d \n", vol / 1000, vol % 1000);
                 value_sum=0;
                 ret = rt_adc_disable(adc_dev,ADC_TEMPER_CHANNEL);
                 rt_thread_mdelay(100);

                 if(vol>1050)//过温保护，估计是NTC是80度左右，到时再测
                 {
                     //停机，报警，故障码
                     pwm_vout_adj(11,0);//超载报警
                     rt_kprintf(" Warning, over temperature!!!\r\n");
                 }

                 rt_thread_mdelay(800);
      }

}

